/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var aeRequest;
var action;
var ajxUrl= "";
var id;
var feedname;
var feedemail;
var feedDes;
var feedcity;
var feedbackType;
var feedbackId;
var rowId;

function createXmlHttpRequestObject() {
    var ro;
    var browser = navigator.appName;
    // Need to determine IE7 and not do this.
    if (browser == "Microsoft Internet Explorer") {
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else {
        ro = new XMLHttpRequest();
    }
    return ro;
}

function createResponseXML(textXML) {
    // code for IE
    if (window.ActiveXObject) {
        var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(textXML);
        return xmlDoc;
    }
    // code for Mozilla, Firefox, Opera, etc.
    else {
        var parser = new DOMParser();
        var xmlDoc = parser.parseFromString(textXML, "text/xml");
        return xmlDoc;
    }
}
function updateFeedbacks(feedbackId, feedname,feedemail,feedDes,feedcity,feedbackType,action){
    var url = '/RadioCabsProject-war/FeedbackServlet?feedbackid='+ feedbackId +'&feedbackName=' +feedname + '&feedEmail=' + feedemail +
    '&feedDescription=' + feedDes + '&feedCity=' + feedcity + '&feedType=' + feedbackType +
    '&action=' + action;
    //alert(url);
    aeRequest = createXmlHttpRequestObject();
    aeRequest.open("POST", url, true);
    aeRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    aeRequest.onreadystatechange = finishUpdate;
    aeRequest.send(null);
}

function finishUpdate(){
    if(aeRequest.readyState==4){
        if(aeRequest.status == 200){
            var response = createResponseXML(aeRequest.responseText);
            var result = response.getElementsByTagName("result")[0].firstChild.nodeValue;
            if (result == "success") {
                window.location.href="home.jsp"; 
            }else{
                alert("System has failure!!!");
            }
        }
    }
}

function randomString() {
    var chars = "0123456789";
    var string_length = 6;
    var randomstring = '';
    for (var i=0; i<string_length; i++) {
        var rnum = Math.floor(Math.random() * chars.length);
        randomstring += chars.substring(rnum,rnum+1);
    }
    return randomstring;
}
String.prototype.trim = function()
{
    var trimmed = this.replace(/^\s+|\s+$/g, '') ;
    return trimmed;
};

function setDeleteFormPosition(){
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = jQuery("#deleteContainer").height();
    var popupWidth = jQuery("#deleteContainer").width();

    jQuery("#deleteContainer").css({
        "position" : "absolute",
        "top" : (windowHeight/2 - popupHeight/2)-50,
        "left" : (windowWidth/2 - popupWidth/2)+150
    });
}

function loginUser(type,username,password){
    actionType = type;
    var url = '/RadioCabsProject-war/LoginServlet?type='+ type +'&username=' +username + '&password=' + password;
    //alert(url);
    aeRequest = createXmlHttpRequestObject();
    aeRequest.open("POST", url, true);
    aeRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    aeRequest.onreadystatechange = resultUser;
    aeRequest.send(null);
}

function logoutUser(){
    var url = '/RadioCabsProject-war/LogoutServlet';
    //alert(url);
    aeRequest = createXmlHttpRequestObject();
    aeRequest.open("POST", url, true);
    aeRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    aeRequest.onreadystatechange = resultUser;
    aeRequest.send(null);
}

function resultUser(){
    if(aeRequest.readyState==4){
        if(aeRequest.status == 200){
            var response = createResponseXML(aeRequest.responseText);
            var result = response.getElementsByTagName("result")[0].firstChild.nodeValue;
            if (result == "success") {
                    window.location.href= "feedback.jsp";
            }else{
                alert("Login failed!");
            }
        }
    }
}

jQuery(document).ready(function() {
    jQuery("#btnSubmitFeedback").live('click',function(){
        feedbackId = "1";
        feedname = jQuery("#txtFName").val();
        feedemail = jQuery("#txtFEmail").val();
        feedDes = jQuery("#txtFDes").val();
        feedcity = jQuery("#txtFCity").val();
        feedbackType = jQuery("#type-feedback").val();
        if(checkvalidateFeedbackForm()){
            updateFeedbacks(feedbackId, feedname, feedemail, feedDes, feedcity, feedbackType, "add")
        }
    });

    jQuery("#btnResetFeedback").live('click',function(){
        jQuery("#txtFName").val("");
        jQuery("#txtFEmail").val("");
        jQuery("#txtFDes").val("");
        jQuery("#txtFCity").val("");
        jQuery("#type-feedback").val("0");
    });


    jQuery(".delForm").live('click',function(){
        id = this.id;
        rowId = jQuery(this).parent().parent().attr('id');
        feedbackId = jQuery(this).parent().parent().find('td:eq(0)').html();
        feedname = jQuery(this).parent().parent().find('td:eq(1)').html();
        feedemail = jQuery(this).parent().parent().find('td:eq(2)').html();
        feedDes = jQuery(this).parent().parent().find('td:eq(3)').html();
        feedcity = jQuery(this).parent().parent().find('td:eq(4)').html();
        feedbackType = jQuery(this).parent().parent().find('td:eq(5)').html();
        if(jQuery("#deleteContainer").is(":hidden")){
            setDeleteFormPosition();
            jQuery("#deleteContainer").slideDown("slow");
        }else{
            jQuery("#deleteContainer").slideUp("slow");
        }
    });

    jQuery("#noDel").live('click',function(){
        jQuery("#deleteContainer").hide("explode",1000);
    });

    jQuery("#yesDel").live('click',function(){
        id = this.id;
        updateFeedbacks(feedbackId, feedname, feedemail, feedDes, feedcity, feedbackType, "delete")

        jQuery("#"+rowId).remove();

        jQuery("#deleteContainer").hide("explode",1000);
    });

    jQuery("#btnLogin").live('click',function(){
        usernameRC = jQuery("#txtUser").val();
        passwordRC = jQuery("#txtPass").val();
        typeRC = jQuery("#listUserType").val();

        loginUser(typeRC, usernameRC, passwordRC);
    });

    jQuery("#btnLogout").live('click',function(){
        logoutUser();
    });

    jQuery("#listUserType").change(function(){
       if(jQuery(this).val() == "Advertise"){
           //jQuery("#txtPass").fadeOut("slow");
           jQuery("#lbPass").fadeOut("slow");
       }else{
           //jQuery("#txtPass").fadeIn("fast");
           jQuery("#lbPass").fadeIn("slow");
       }
    });

    jQuery("#txtUser").keypress(function(event){
        if(event.which == 13){
            usernameRC = jQuery("#txtUser").val();
            passwordRC = jQuery("#txtPass").val();
            typeRC = jQuery("#listUserType").val();

            loginUser(typeRC, usernameRC, passwordRC);
        }
    })

    jQuery("#txtPass").keypress(function(event){
        if(event.which == 13){
            usernameRC = jQuery("#txtUser").val();
            passwordRC = jQuery("#txtPass").val();
            typeRC = jQuery("#listUserType").val();

            loginUser(typeRC, usernameRC, passwordRC);
        }
    })
});


